from rest_framework import permissions


class UserInfoSelfOrReadOnly(permissions.BasePermission):
    message = '您正试图修改他人的私有信息, 而您无权这么做'

    def has_object_permission(self, request, view, obj):
        """
        只有自己可以编辑自己的信息
        """
        if request.method in permissions.SAFE_METHODS:
            return True
        return obj == request.user


class UserQuestionSelfEdit(permissions.BasePermission):
    message = '您无权操作他人创建的题目'

    """
    只有自己可以修改自己创建的题目
    """

    def has_object_permission(self, request, view, obj):
        if request.method not in ('PUT', 'DELETE'):
            return True
        return request.user == obj.creator
